/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package asmencoder;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 *
 * @author wei
 */
public class ASMencoder {

    /**
     * @param args the command line arguments
     */
    static String[] binary3 = {"000","001","010","011","100","101","110", "111"};

    static String toBinary3(int value) {
        return binary3[value & 0xF];
    }
    
    
    static String Encoder(String[] AA){
        String MC = null;
        if(AA[0].equals("MOV")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "00100"+p1str+"00000000"+p2str;
            System.out.println("MOV"+MC);  
            
        }
        else if(AA[0].equals("SET")) {
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = Integer.toBinaryString(0x100 | p1value).substring(1);
            MC = "00011"+ p1str +"000"+p2str;
            System.out.println("SET"+MC);
        }
        
        else if(AA[0].equals("LOAD")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "00101"+p1str+"00000000"+p2str;
            System.out.println("LOAD"+MC);  
            
        }
        else if(AA[0].equals("SAVE")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "00110"+"000"+p1str+"00000"+p2str;
            System.out.println("SAVE"+MC);  
            
        }        
        else if(AA[0].equals("ADD")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = toBinary3(p3value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "00111"+p1str+p2str+"00000"+p3str;
            System.out.println("ADD"+MC);  
            
        }
        else if(AA[0].equals("SUB")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = toBinary3(p3value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01000"+p1str+p2str+"00000"+p3str;
            System.out.println("SUB"+MC);  
            
        }
        else if(AA[0].equals("MUL")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = toBinary3(p3value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01001"+p1str+p2str+"00000"+p3str;
            System.out.println("MUL"+MC);  
            
        }
        else if(AA[0].equals("XOR")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = toBinary3(p3value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01010"+p1str+p2str+"00000"+p3str;
            System.out.println("XOR"+MC);  
            
        }        
        else if(AA[0].equals("JUMP")){
            int p1value = Integer.decode(AA[1]);
            String p1str = Integer.toBinaryString(0x1000 | p1value).substring(1);
            MC = "1010000"+ p1str;
            System.out.println("JUMP"+MC);
        }        
        else if(AA[0].equals("ADDI")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = Integer.toBinaryString(0x100 | p3value).substring(1);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01011"+p1str+p2str+p3str;
            System.out.println("ADDI"+MC);  
            
        }
        else if(AA[0].equals("SUBI")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = Integer.toBinaryString(0x100 | p3value).substring(1);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "011100"+p1str+p2str+p3str;
            System.out.println("SUBI"+MC);  
            
        }
        else if(AA[0].equals("MULI")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            int p3value = Character.getNumericValue(AA[3].charAt(1));
            String p3str = Integer.toBinaryString(0x100 | p3value).substring(1);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01101"+p1str+p2str+p3str;
            System.out.println("MULI"+MC);  
            
        }
        else if(AA[0].equals("EQ")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01110"+"000"+p1str+"00000"+p2str;
            System.out.println("EQ"+MC);  
            
        }
        else if(AA[0].equals("NEQ")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "01111"+"000"+p1str+"00000"+p2str;
            System.out.println("NEQ"+MC);  
            
        }
        else if(AA[0].equals("GT")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "10000"+"000"+p1str+"00000"+p2str;
            System.out.println("GT"+MC);  
            
        }
        else if(AA[0].equals("LT")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "10001"+"000"+p1str+"00000"+p2str;
            System.out.println("LT"+MC);  
            
        }
        else if(AA[0].equals("GE")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "10010"+"000"+p1str+"00000"+p2str;
            System.out.println("GE"+MC);  
            
        }
        else if(AA[0].equals("LE")){
            int p1value = Character.getNumericValue(AA[1].charAt(1));
            String p1str = toBinary3(p1value);
            int p2value = Character.getNumericValue(AA[2].charAt(1));
            String p2str = toBinary3(p2value);
            //int P3value = Character.getNumericValue(AA[3].charAt(1));
            
            MC = "10011"+"000"+p1str+"00000"+p2str;
            System.out.println("LE"+MC);  
            
        }
        else if(AA[0].equals("JT")){
            int p1value = Integer.decode(AA[1]);
            String p1str = Integer.toBinaryString(0x1000 | p1value).substring(1);
            MC = "1010100"+ p1str;
            System.out.println("JT"+MC);
        }
        else if(AA[0].equals("JF")){
            int p1value = Integer.decode(AA[1]);
            String p1str = Integer.toBinaryString(0x1000 | p1value).substring(1);
            MC = "1011000"+ p1str;
            System.out.println("JF"+MC);
        }
        else if(AA[0].equals("HALT")){
            
            MC = "1111111111111111111";
            System.out.println("HALT"+MC);
        }
        else if(AA[0].equals("VID")){
            MC = "0000000000000000000";
            System.out.println("VID"+MC);
        }
        else if(AA[0].equals("AUD")){
            int p1value = Integer.decode(AA[1]);
            String p1str = toBinary3(p1value);
            MC = "00001"+ "00000000000"+p1str;
            System.out.println("AUD"+MC);
        }
        else System.out.println("no match!!!");
        return MC;
    }
    
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here

        String [] code = new String [3];
        ArrayList<String> SA = new ArrayList<String>();
        InputStream fis;
        BufferedReader br;
        String line = null;
        int index = 0;
        PrintWriter writer = new PrintWriter("machineCode.txt", "UTF-8");
        fis = new FileInputStream("Instructions.txt");
        br = new BufferedReader(new InputStreamReader(fis));
        while((line = br.readLine()) != null){
            
            code = line.split(", ");
            writer.println("( line == " + Integer.toString(index) + ") ? 19'b"+Encoder(code)+": //" + line);
            index ++;
        }
        fis.close();
        writer.close();
        
        
        
    }
    
}
